package com.zg.scf.service.impl;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zg.common.core.dao.scf.entity.Rule;
import com.zg.common.core.dao.scf.RuleDao;
import com.zg.common.core.service.PageBaseService;
import com.zg.scf.domain.req.rule.RulePageReq;
import com.zg.scf.domain.req.rule.RuleStateEditReq;
import com.zg.scf.domain.vo.ConfCompanyVo;
import com.zg.scf.service.RuleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>Description: </p>
 *
 * @author zhanfeng.liang
 * @since 2025/5/22 10:47
 */

@Slf4j
@Service
public class RuleServiceImpl extends PageBaseService<Rule, RuleDao> implements RuleService {

//    @Autowired
//    @Qualifier("confCompanyDao")
//    @Override
//    protected void setDao(IDao<ConfCompany> dao) {
//        this.dao = dao;
//    }

    public  LambdaQueryWrapper<Rule> buildQueryWrapper(Rule confCompany){
        LambdaQueryWrapper<Rule> qw = new LambdaQueryWrapper<>();
        if(confCompany!=null){
//            qw.like(!StrUtil.isEmpty(confCompany.getCompanyName()), ConfCompany::getCompanyName, confCompany.getCompanyName());
            qw.eq(!StrUtil.isEmpty(confCompany.getState()), Rule::getState, confCompany.getState());
            qw.eq(!StrUtil.isEmpty(confCompany.getDirection()), Rule::getDirection, confCompany.getDirection());
//            if(confCompany.getBeginTime()!=null){
//                qw.and(wq -> wq.between(ConfCompany::getBeginTime, confCompany.getBeginTime(), confCompany.getEndTime()).or().between(ConfCompany::getEndTime, confCompany.getBeginTime(), confCompany.getEndTime()));
//            }
            qw.ge(confCompany.getBeginTime()!=null, Rule::getEndTime, confCompany.getBeginTime());
            qw.le(confCompany.getEndTime()!=null, Rule::getBeginTime, confCompany.getEndTime());
        }
        return qw;
    }


    @Override
    public Page<ConfCompanyVo> pageInfo(RulePageReq req, Class<ConfCompanyVo> voClass) {
        Page<Rule> page = new Page<>(req.getPage(), req.getPageSize());
        LambdaQueryWrapper<Rule> qw = buildPageQueryWrapper(req);
        Page<Rule> pageInfo = baseDao.pageInfo(page, qw,req.getCompanyName());

        List<ConfCompanyVo> voList = pageInfo.getRecords().stream()
                .map(entity -> convertToVo(entity, voClass))
                .collect(Collectors.toList());

        Page<ConfCompanyVo> voPage = new Page<>(pageInfo.getCurrent(), pageInfo.getSize(), pageInfo.getTotal());
        voPage.setRecords(voList);

        return voPage;
    }

    public void editState(RuleStateEditReq editReq){
        Rule confCompany = getById(editReq.getId());
        confCompany.setState(editReq.getState());
        baseDao.updateById(confCompany);
    }
}
